'''

拼多多评价以及商品属性采集

/*PHP是世界上最好的语言*/

'''

import requests,re,time,os
from base_config import *
from manage.exception import Exceptions

class GoodsComment( object ):

    def __init__(self):

        # 匹配购买类型
        self.pattern_key = re.compile(r'"spec_key":"(.*?)"')
        self.pattern_value = re.compile(r'"spec_value":"(.*?)"')

        # 匹配商品款式
        self.pattern_style = re.compile(r'rawData= (.*);')

    # 评论买家购买款式
    def requestIndex( self,params ):


        page = int(params['page']) +1

        r = requests.get( domain_url['other']['pdd_comments_url'].format(goodsid=params['goodsid'],page=page ), headers=header )

        # ip被检测 返回失败状态
        if r.status_code in [403,429]:

            data = {

                'id': params['tasksid']
            }

            text = lambda: requests.post(url=domain_url['domain']['test_failComm_url'], data=data)

            if text().status_code == 200 and text().json()['code'] == 0:

                print('[商品SKU采集任务] 返回失败任务成功:', params['tasksid'])


            # 记录失败页数
            print('[商品SKU采集任务]:IP被检测 等待程序重新启动:%s' % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))))

            os._exit(0)

        if r.status_code == 200:

            # 判断商品是否有评价信息
            if len(r.json()['data']) == 0:

                print('[商品SKU采集任务]:  商品评价任务采集完成:', params['tasksid'])
                # 返回任务成功
                suData = {'id': params['tasksid']}

                Exceptions.successTask(suData)

                return False

            return r.json()

    # 买家购买款式解析
    def parseJson( self , params ):

        # page 评价页数

        page = int( params['page'] )


        if isinstance( self.requestIndex( params ),dict):

            while True:

                page += 1

                params['page'] = str(page)

                text = self.requestIndex( params )
                #没有评价信息
                if not text:

                    return

                items = text['data']

                if len(items) == 0:

                    # 重置页数
                    page = 0

                    break


                for item in items:

                    # 判断是否有当天评价信息

                    if  params['startTime']< item['time'] <= params['endTime']:

                        result = item['specs']

                        # 如果没有sku直接返回任务成功 备注 以字符串'[]'判断
                        if len( result ) == 2:

                            print('[商品SKU采集任务] %s\t该商品无SKU规格信息'%params['keywords'])

                            # print('商品评价任务采集完成:', params['tasksid'])
                            # 返回任务成功
                            suData = {'id': params['tasksid']}

                            Exceptions.successTask(suData)

                            break


                        keys = re.findall(self.pattern_key, result)

                        values = re.findall(self.pattern_value, result)

                        result = "{0}:{1}".format(keys[0], values[0])

                        data = {

                            "id": params['tasksid'],
                            "goodsid": params['goodsid'],
                            "page": str(page),
                            "judge_class": result
                        }

                        # 上传数据
                        self.apiRequest(data)

                    else:

                        print('[商品SKU采集任务]: %s\t商品SKU规格信息采集完成'%params['keywords'])

                        # 返回任务成功
                        suData = {'id': params['tasksid']}

                        Exceptions.successTask(suData)

                        break

                break

    # 数据上传
    def apiRequest(self,params):

        r = requests.post( url = domain_url['domain']['test_detail_url'], data=params )

        if r.status_code == 200 and r.json()['msg'] == "ok":

            print('[商品SKU采集任务]: 上传商品评价详情成功',params['judge_class'])

        return None

if __name__ == '__main__':

    g = GoodsComment()

    data = {'id': '2', 'sortby': None, 'addby': None, 'createtime': '2019-08-21 11:44:14', 'editby': None, 'edittime': None, 'type': '4', 'keywords': None, 'classcode': None, 'goodsid': '5280947648', 'task_start_time': '0000-00-00 00:00:00', 'task_end_time': '0000-00-00 00:00:00', 'status': '0', 'recordscount': None, 'memo': '善存佳维片1.33g/片*60片*1瓶成人男女士复合维生素多种维生素', 'page': '0', 'processcode': 'A174EF3B-3478-2571-7E28-D75423A4F0C8', 'startTime': 1566316800, 'endTime': 1566403199}



    params = {

        'tasksid':data['id'],
        'keywords':data['memo'],
        'goodsid':data['goodsid'],
        'page':data['page'],
        'startTime':data['startTime'],
        'endTime':data['endTime']

    }

    # print( params )

    g.parseJson( params )











